
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.gruposp2p.aularest.model.Coursegroup;
import org.gruposp2p.aularest.model.Student;
import org.gruposp2p.aularest.model.controller.CoursegroupJpaController;
import org.gruposp2p.aularest.model.controller.StudentJpaController;
import org.gruposp2p.aularest.model.controller.exceptions.PreexistingEntityException;
import org.gruposp2p.aularest.utils.DateUtils;

/**
 *
 * @author jj
 */

public class InsertStudent {

    public static void main (String[] args) throws Exception {
        InsertStudent insertStudent = new InsertStudent();
        insertStudent.insertStudent();
    }

    public void insertStudent () {
        try {
            CoursegroupJpaController grupoJpaController = new CoursegroupJpaController();
            List<Coursegroup> groups = grupoJpaController.findCoursegroupEntities();
            Coursegroup group = groups.get(0);
            Student student = new Student();
            student.setName("NombreStudent1");
            student.setSurnames("SurnameStudent1");
            student.setPhone1("5555555");
            student.setCoursegroupId(group);
            Date date = DateUtils.getTodayDate();
            student.setBirthdate(date);
            StudentJpaController studentJpaController = new StudentJpaController();
            studentJpaController.create(student);
            System.out.println("student.getId(): " + student.getId());
            List<Student> students = studentJpaController.findStudentEntities();
            System.out.println("students.size(): " + students.size());
            /*
            Course course = null;
            panelEnabled = initCourseComboboxData ();
            if (student != null) {
            Coursegroup coursegroup = student.getCoursegroupId();
            if (coursegroup != null) {
            course = student.getCoursegroupId().getCourseId();
            panelEnabled = AulaComboBoxUtils
            .updateCourseAndCoursegroupComboBoxData(
            course, coursegroupComboBox, courseComboBox);
            coursegroupComboBox.setSelectedItem(coursegroup);
            }
            String name = student.getName();
            if (name != null) studentNameTextField.setText(name);
            String surnames = student.getSurnames();
            if (surnames != null) studentSurnamesTextField.setText(surnames);
            byte [] photoData = student.getPhoto();
            setPhoto(photoData);
            String phone1 = student.getPhone1();
            if (phone1 != null) phone1TextField.setText(phone1);
            String phone2 = student.getPhone2();
            if (phone2 != null) phone2TextField.setText(phone1);
            Date date = student.getBirthdate();
            if (date != null) datePicker.setDate(date);
            String email = student.getEmail();
            if (email != null) {
            emailTextField.setText(email);
            emailTextField.setCaretPosition(0);
            }
            Collection<Address> addresses = student.getAddressCollection();
            if (addresses != null && addresses.size() > 0)
            addressPanel.setData(addresses.iterator().next());
            setEnabled(panelEnabled);
            courseComboBox.setEnabled(true);*/
        } catch (PreexistingEntityException ex) {
            Logger.getLogger(InsertStudent.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(InsertStudent.class.getName()).log(Level.SEVERE, null, ex);
        }


        /*
        Course course = null;
        panelEnabled = initCourseComboboxData ();
        if (student != null) {
            Coursegroup coursegroup = student.getCoursegroupId();
            if (coursegroup != null) {
                course = student.getCoursegroupId().getCourseId();
                panelEnabled = AulaComboBoxUtils
                        .updateCourseAndCoursegroupComboBoxData(
                        course, coursegroupComboBox, courseComboBox);
                coursegroupComboBox.setSelectedItem(coursegroup);
            }
            String name = student.getName();
            if (name != null) studentNameTextField.setText(name);
            String surnames = student.getSurnames();
            if (surnames != null) studentSurnamesTextField.setText(surnames);
            byte [] photoData = student.getPhoto();
            setPhoto(photoData);
            String phone1 = student.getPhone1();
            if (phone1 != null) phone1TextField.setText(phone1);
            String phone2 = student.getPhone2();
            if (phone2 != null) phone2TextField.setText(phone1);
            Date date = student.getBirthdate();
            if (date != null) datePicker.setDate(date);
            String email = student.getEmail();
            if (email != null) {
                emailTextField.setText(email);
                emailTextField.setCaretPosition(0);
            }
            Collection<Address> addresses = student.getAddressCollection();
            if (addresses != null && addresses.size() > 0)
                addressPanel.setData(addresses.iterator().next());
            setEnabled(panelEnabled);
            courseComboBox.setEnabled(true);*/


    }

}
